﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Net.Sockets;
using System.Threading;
using System.Xml;

namespace DBClient
{
    class Program
    {
        static Int32 port = 10000;
        static Socket socket;
        static string hostname;
        static int buffersize;
        static void LoadConfig()
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load("config.xml");
                XmlNodeList xmlNode = xmlDoc.GetElementsByTagName("hostname");
                hostname = xmlNode[0].InnerText;
                xmlNode = xmlDoc.GetElementsByTagName("port");
                port = Int32.Parse(xmlNode[0].InnerText);
                xmlNode = xmlDoc.GetElementsByTagName("buffersize");
                buffersize = Int32.Parse(xmlNode[0].InnerText);
            }
            catch (System.Exception e)
            {
                Console.Write(e.Message);
            }
        }
        static void Main(string[] args)
        {
            LoadConfig();
            IPAddress ipAddress = Dns.Resolve(hostname).AddressList[0];
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                socket.Connect(ipAddress, port);
            }
            catch (System.Exception e)
            {
                Console.WriteLine("Connect failed!");
                return;
            }
            
            if (socket.Connected)
            {
                Console.WriteLine("Connect to server {0} : {1} done", ipAddress, port);
            }
            else
            {
                Console.WriteLine("Connect to server failed, please check config.xml");
                return;
            }
            socket.ReceiveBufferSize = 100000;
            while (true)
            {
                try
                {
                    NetworkStream networkStream = new NetworkStream(socket);
                    StreamWriter streamWriter = new StreamWriter(networkStream);
                    StreamReader streamReader = new StreamReader(networkStream);

                    string line = System.Console.ReadLine();

                    switch (line)
                    {
                        case "cls":
                            Console.Clear();
                            continue;
                        case "exit":
                            streamWriter.WriteLine("exit");
                            streamWriter.Flush();
                            socket.Close(3000);
                            return;
                        case "bye":
                            return;
                        default:
                            break;

                    }
                    Thread th = new Thread(new ThreadStart(listen));
                    th.Start();

                    streamWriter.WriteLine(line);
                    streamWriter.Flush();

                }
                catch (System.Exception e)
                {

                }
            }

        }

        static void listen()
        {
            byte[] tmp = new byte[buffersize];
            int length = 0;
            try
            {
                length = socket.Receive(tmp);
            }
            catch (System.Exception e)
            {
                
                return;
            }
            Console.Write(Encoding.UTF8.GetString(tmp).Substring(0, length).Trim());
            if (length > 0)
            {
                Thread th = new Thread(new ThreadStart(listen));
                th.Start();
            }else
            {
                Console.WriteLine();
            }
        }
    }
}
